package com.chenglu.android.order.mvp.presenter;


import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RetrofitClient;
import com.chenglu.android.common.net.RetrofitClientCL;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.CommonDataBean;
import com.chenglu.android.order.bean.OrderInfoBean;
import com.chenglu.android.order.mvp.contract.OrderContract;
import com.chenglu.android.order.mvp.model.OrderModel;
import com.chenglu.android.orderinfo.bean.OrderDetailBean;
import com.chenglu.android.orderinfo.bean.QrcodeBean;
import com.chenglu.android.payment.bean.ComfirmOrderBean;
import com.chenglu.android.payment.bean.CouponListBean;
import com.chenglu.android.payment.bean.PayOrderBean;
import com.chenglu.android.payment.bean.PayTypeConfig;
import com.chenglu.android.payment.bean.TenantIdPayMethod;
import com.chenglu.android.search.bean.SearchBean;
import com.google.zxing.qrcode.encoder.QRCode;

import java.util.Map;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.RequestBody;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class OrderPresenter extends BasePresenter<OrderContract.View> implements OrderContract.OrderPresenter {

     private  OrderContract.OrderModel model;

    public OrderPresenter(){
        model = new OrderModel();
    }


    @Override
    public void getCouponList(String token, RequestBody requestBody) {
        if(!isViewAttached()){
            return;
        }

        model.getCouponList(token,requestBody)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CouponListBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CouponListBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getCouponList(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });

    }



    @Override
    public void getenantIdPayMethod(String token, String payClient, String tenantId) {
        if(!isViewAttached()){
            return;
        }

        model.getenantIdPayMethod(token,payClient,tenantId)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<TenantIdPayMethod>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull TenantIdPayMethod bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getenantIdPayMethod(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


    @Override
    public void getPayOrder(String token, RequestBody requestBody) {
        if(!isViewAttached()){
            return;
        }

        model.getPayOrder(token,requestBody)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PayOrderBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull PayOrderBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getPayOrder(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getOrderList(String token, Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }

        model.getOrderList(token,map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<OrderInfoBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull OrderInfoBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getOrderList(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getOrderCancel(String token, String id) {
        if(!isViewAttached()){
            return;
        }

        model.getOrderCancel(token,id)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CommonDataBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CommonDataBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getOrderCancel(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getOrderRemove(String token, String ids) {
        if(!isViewAttached()){
            return;
        }

        model.getOrderRemove(token,ids)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CommonDataBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CommonDataBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getOrderRemove(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getOrderReceiving(String token, String id) {
        if(!isViewAttached()){
            return;
        }

        model.getOrderReceiving(token,id)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CommonDataBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CommonDataBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getOrderReceiving(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getOrderInfo(String token,String id,int shopsTypeId) {

        if(!isViewAttached()){
            return;
        }

        //    case none = 0                /// 空
//    case shop = 1                /// 商超
//    case restaurant = 2          /// 餐饮
//    case beauty = 3              /// 美业
//    case education = 4           /// 教育
//    case minimart = 8            /// 便利店
//    case pharmaceuticals = 9     /// 药店
//    case breed = 10              /// 孕育
//    case max = 999               /// 平台公共

        switch (shopsTypeId){
            case Constant.typeIdShop:

                setInfoRequest(model.getOrderInfo(token,id));
                break;
            case Constant.typeIdRestaurant:
                setInfoRequest(model.getRestautantInfo(token,id));
                break;
            case Constant.typeIdBeauty:

                setInfoRequest(model.getBeautyInfo(token, id));
                break;
            case Constant.typeIdEducation:

                setInfoRequest(model.getEducationInfo(token, id));
                break;
            case Constant.typeIdMinimart:

                setInfoRequest(model.getMinimartInfo(token, id));
                break;
            case Constant.typeIdPharmaceuticals:

                setInfoRequest(model.getPharmaceuticalsInfo(token, id));
                break;
            case Constant.typeIdBreed:

                setInfoRequest(model.getOrderBreedInfo(token, id));
                break;


        }


//        model.getOrderInfo(token,id)
//                .compose(RxScheduler.Obs_io_main())
//                .to(mView.bindAutoDispose())
//                .subscribe(new Observer<OrderDetailBean>() {
//                    //订阅
//                    @Override
//                    public void onSubscribe(@NonNull Disposable d) {
//                        mView.showLoading();
//                    }
//
//                    @Override
//                    public void onNext(@NonNull OrderDetailBean bean) {
//                        //每次进来都会刷新token
//                        switch (bean.getCode()) {
//                            case Constant.HTTPCODE200:
//                                mView.getOrderInfo(bean);
//                                break;
//                            case Constant.HTTPCODE400:
//                                mView.onEmpty(bean.getMsg());
//                                ToastUtils.show(bean.getMsg());
//                                break;
//                            case Constant.HTTPCODE401:
//                                ToastUtils.show(bean.getMsg());
//                                break;
//                            case Constant.HTTPCODE404:
//                                ToastUtils.show(bean.getMsg());
//                                break;
//                            case Constant.HTTPCODE500:
//                                ToastUtils.show(bean.getMsg());
//                                break;
//                        }
//                    }
//
//                    @Override
//                    public void onError(@NonNull Throwable e) {
//                        mView.dismissLoading();
//                        NetErrorException.apiError(e,mView);
////                          ToastUtils.show(e.getMessage());
//                        LogUtils.v(e.getMessage());
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        mView.dismissLoading();
//
//                    }
//                });
    }

    @Override
    public void getQrCodeInfo(String token, String id, int shopsTypeId) {
        if(!isViewAttached()){
            return;
        }
        if(shopsTypeId == Constant.typeIdBeauty){
            setQrCodeRequest(model.getBeautyQrcode(token, id));
        }else if(shopsTypeId == Constant.typeIdEducation){
            setQrCodeRequest(model.getEducationQrcode(token, id));
        }
    }

    private void setInfoRequest(Observable<OrderDetailBean> orderBreedInfo) {


        orderBreedInfo.compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<OrderDetailBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull OrderDetailBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getOrderInfo(bean);
                                break;
                            case Constant.HTTPCODE400:
                                mView.onEmpty(bean.getMsg());
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e, mView);
//                          ToastUtils.show(e.getMessage());
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


    private void setQrCodeRequest(Observable<QrcodeBean> qrCodeRequest) {


        qrCodeRequest.compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<QrcodeBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull QrcodeBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getQrCodeInfo(bean);
                                break;
                            case Constant.HTTPCODE400:
                                mView.onEmpty(bean.getMsg());
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e, mView);
//                          ToastUtils.show(e.getMessage());
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

}
